import $web3js from "../web3.js";
import $usdt from "../usdt/index.js";

const contractInfo = require("./ifilMiningContract.json");
let contractAddress = "";

let web3 = $web3js.getWeb3();
let contractConst = {
  contract: web3 ? new web3.eth.Contract(contractInfo.abi, contractAddress) : ""
};
const getIfilAddress = () => {
  return contractAddress;
};

const getIfilContract = () => {
  return contractConst.contract;
};

const createNFT = async (_inviterid, _prodid, _buyAmount, _payment) => {
  _payment = await $usdt.toUsdtValue(_payment);
  _inviterid = $web3js.stringToBytes32(_inviterid);
  return $web3js.commonSend(
    getIfilContract().methods.createNFT,
    _inviterid,
    _prodid,
    _buyAmount,
    _payment
  );
};

const createNFTEstimateGas = async (
  _inviterid,
  _prodid,
  _buyAmount,
  _payment
) => {
  _payment = await $usdt.toUsdtValue(_payment);
  _inviterid = $web3js.stringToBytes32(_inviterid);
  return $web3js.commonEstimateGas(
    getIfilContract().methods.createNFT,
    _inviterid,
    _prodid,
    _buyAmount,
    _payment
  );
};

const withdrawBenefits = () => {
  return $web3js.commonSend(getIfilContract().methods.withdrawBenefits);
};

const withdrawBenefitsEstimateGas = () => {
  return $web3js.commonEstimateGas(getIfilContract().methods.withdrawBenefits);
};

const withdrawIFil = (_amount, _iflAddr) => {
  _amount = $web3js.toWei(String(_amount));
  return $web3js.commonSend(
    getIfilContract().methods.withdrawIFil,
    _amount,
    _iflAddr
  );
};

const withdrawIFilEstimateGas = (_amount, _iflAddr) => {
  _amount = $web3js.toWei(String(_amount));
  return $web3js.commonEstimateGas(
    getIfilContract().methods.withdrawIFil,
    _amount,
    _iflAddr
  );
};

const getInviter = _inviterid => {
  _inviterid = $web3js.stringToBytes32(_inviterid);
  return $web3js.commonCall(getIfilContract().methods.getInviter, _inviterid);
};

const userRegister = _inviterid => {
  _inviterid = $web3js.stringToBytes32(_inviterid);
  return $web3js.commonSend(getIfilContract().methods.userRegister, _inviterid);
};

const userRegisterEstimateGas = _inviterid => {
  _inviterid = $web3js.stringToBytes32(_inviterid);
  return $web3js.commonEstimateGas(
    getIfilContract().methods.userRegister,
    _inviterid
  );
};

const getAndCheckRecvAirdrop = owner => {
  return $web3js.commonCall(
    getIfilContract().methods.getAndCheckRecvAirdrop,
    owner
  );
};

const recvAirdrop = () => {
  return $web3js.commonSend(getIfilContract().methods.recvAirdrop);
};

const recvAirdropEstimateGas = () => {
  return $web3js.commonEstimateGas(getIfilContract().methods.recvAirdrop);
};

export default {
  getIfilAddress,
  getIfilContract,
  createNFT,
  createNFTEstimateGas,
  withdrawBenefits,
  withdrawBenefitsEstimateGas,
  withdrawIFil,
  withdrawIFilEstimateGas,
  userRegister,
  userRegisterEstimateGas,
  getInviter,
  getAndCheckRecvAirdrop,
  recvAirdrop,
  recvAirdropEstimateGas
};
